package com.zline.foodraise.ui.activity;

import android.view.View;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.baidu.mobstat.StatService;
import com.zline.foodraise.R;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.AppPref_;
import com.zline.foodraise.util.LUtil;
import com.zline.foodraise.util.StringUtils;
import com.zline.foodraise.wx.WxShareUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.sharedpreferences.Pref;

/**
 * Created by heshantao on 15/9/17.
 * 模板分享
 */
@EActivity(R.layout.activity_template_share)
public class TemplateShareActivity extends ZlineActivity {
    private static final String LOG_TAG = TemplateShareActivity.class.getSimpleName();
    //合成后的模板图片本地存储地址
    @Extra
    String templatePath;
    @Bean
    WxShareUtil wxShareUtil;
    //分享结果
    //@Extra
    String shareResult;
    MaterialDialog shareDialog;
    @Pref
    AppPref_ appPref;

    //微信分享
    void wechatShare() {
        wxShareUtil.regToWx();
        wxShareUtil.setShareContentType(Constant.PIC_SHARE_TYPE);
        wxShareUtil.setShareChannel(0);
        wxShareUtil.setSharePicPath(templatePath);
        wxShareUtil.sendPicReq();
    }

    //分享到朋友圈
    void timelineShare() {
        wxShareUtil.regToWx();
        wxShareUtil.setShareContentType(Constant.PIC_SHARE_TYPE);
        wxShareUtil.setShareChannel(1);
        wxShareUtil.setSharePicPath(templatePath);
        wxShareUtil.sendPicReq();
    }

    @Override
    protected void onResume() {
        super.onResume();
        shareResult=appPref.shareResult().get();
        if(!StringUtils.isBlank(shareResult)){
            showShareResultDialog();
            appPref.edit().shareResult().put("").apply();
        }
    }

    void showShareResultDialog() {
        shareDialog = new MaterialDialog.Builder(this)
                .title(R.string.share)
                .titleGravity(GravityEnum.CENTER)
                .content(shareResult)
                .positiveText(R.string.ok)
                .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onNegative(MaterialDialog dialog) {
                        super.onNegative(dialog);
                    }

                    @Override
                    public void onPositive(MaterialDialog dialog) {
                        super.onPositive(dialog);
                        shareDialog.dismiss();
                    }
                })
                .show();
    }


    @Click({R.id.header_back_iv, R.id.header_home_rl,
            R.id.share_wechat_tv, R.id.share_timeline_tv})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_home_rl:
                MainActivity_.intent(this).start();
                break;
            case R.id.share_wechat_tv:
                StatService.onEvent(this, "share_pic_wx_friend", "分享模板图片给微信好友");
                wechatShare();
                break;
            case R.id.share_timeline_tv:
                timelineShare();
                StatService.onEvent(this, "share_pic_wx_cycle", "分享模板图片给微信朋友圈");
                break;
            case R.id.header_back_iv:
                goback();
                break;
            default:
                break;
        }
    }

}
